﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace web.httpfetch
{
    public static class HtmlParserHelper
    {

        #region static method for html Parser
        public static string getHtmlFile(string urlString, string encodingString)
        {
            StringBuilder sb = new StringBuilder();

            // prepare the web page we will be asking for
            HttpWebRequest request = (HttpWebRequest)
                WebRequest.Create(urlString);

            // execute the request
            HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();

            Stream resStream = response.GetResponseStream();

            byte[] buf = new byte[8192];

            string tempString = null;
            int count = 0;
            do
            {
                // fill the buffer with data
                count = resStream.Read(buf, 0, buf.Length);

                // make sure we read some data
                if (count != 0)
                {

                    // translate from bytes to ASCII text
                    tempString = Encoding.GetEncoding(encodingString).GetString(buf, 0, count);
                    // continue building the string
                    sb.Append(tempString);
                }
            }
            while (count > 0); // any more data to read?
            return sb.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="temp"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public static string getProperty(string temp, int start, string property)
        {
            bool pair = false;
            int end = moveToThisTagEnd(temp, start);
            string tempstring = temp.Substring(start + 1, end - start);
            if (tempstring.Contains(property))
            {
                start = tempstring.IndexOf(property) + property.Length;
                start = moveToEqualMark(tempstring, start);
                end = start + 1;
                end = start = skipBlanks(tempstring, end); 
                while (tempstring[end] != ' ' && tempstring[end] != '>' && !(tempstring[end] == '/' && tempstring[end + 1] == '>'))
                    end++;
                return tempstring.Substring(start, end - start).Replace("\"", "").Replace("\'", ""); ;
            }
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="TagName"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public static int getTagContent(string TagName, int start)
        {
            Console.WriteLine(TagName);
            return 0;
        }

        public static int moveToEqualMark(string temp, int start)
        {
            for (; temp[start] != '='; start++);
            return start;
        }

        #region Move to Next Tag/Get Next Tag
        public static int moveToNextTagStart(string temp, int start)
        {
            for (; temp[start] != '<'; start++) ;
            return start;
        }

        public static int moveToNextTagEnd(string temp, int start)
        {
            if (temp[start] == '<')
                start++;
            for (; temp[start] != '<'; start++) ;
            for (; temp[start] != '>'; start++) ;
            return start;
        }

        //public static Tags GetNextTag()
        //{
        //    Tags t = new Tags();

        //    return t;
        //}
        #endregion Move to Next Tag

        public static int moveToThisTagEnd(string temp, int start)
        {
            for (; temp[start] != '>'; start++);
            return start;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="temp"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public static string getTagName(string temp, int start)
        {
            int end = start + 1;
            for (;temp[start] != ' ' && temp[start] != '>';start++);
            return temp.Substring(end + 1, start - end - 1);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="temp"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public static TagsType getTagType(string temp, int start)
        {
            int end = start;
            for (; temp[end] != '>';end++)
            {
                if (temp[end] == '/')
                    return TagsType.SingleCloseTags;
            }

            return TagsType.DoubleCloseTags;
        }

        public static int skipBlanks(string temp, int postion)
        {
            for (;temp[postion] == ' '; postion++);
            return postion;
        }
        #endregion
    }
}
